package kr.saeildamil.androidstudy_second20.common;

import kr.saeildamil.androidstudy_second.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CounterFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//방법1
		/*View root=inflater.inflate(R.layout.ch20_counterfragment, container);
		final TextView textcounter=(TextView) root.findViewById(R.id.ch20_counterfragment_counter);
		root.findViewById(R.id.ch20_counterfragment_increase).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				int count=Integer.parseInt(textcounter.getText().toString());
				textcounter.setText(Integer.toString(count+1));
			}
		});*/
		
		//방법2
		/*LinearLayout root=new LinearLayout(getActivity());
		root.setOrientation(LinearLayout.VERTICAL);
		root.setBackgroundColor(Color.YELLOW);
		root.setGravity(Gravity.CENTER_HORIZONTAL);
		
		final TextView textCounter=new TextView(getActivity());
		textCounter.setText("0");
		textCounter.setTextColor(Color.RED);
		textCounter.setTextSize(40);
		
		Button btnIncrease=new Button(getActivity());
		btnIncrease.setText("Increase");
		
		LinearLayout.LayoutParams param=
				new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		root.addView(textCounter,param);
		root.addView(btnIncrease, param);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				int count=Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toString(count+1));
			}
		});*/
		
		//방법3 ---> 코드 실행하면 다운
		/*View root=inflater.inflate(R.layout.ch20_counterfragment, container);
		Button btnIncrease=(Button) getActivity().findViewById(R.id.ch20_counterfragment_increase);
		final TextView textCounter=(TextView) getActivity().findViewById(R.id.ch20_counterfragment_counter);
		btnIncrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count=Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toBinaryString(count+1));
			}
		});*/
		
		View root=inflater.inflate(R.layout.ch20_counterfragment, container);
		return root;
	}//end of onCreateView
	//방법4(3번 방법의 해결책-onActivityCreated에서 구현 getactivity())--가장 좋은 방법
	/*@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Button btnIncrease=(Button) getActivity().findViewById(R.id.ch20_counterfragment_increase);
		final TextView textCounter=(TextView) getActivity().findViewById(R.id.ch20_counterfragment_counter);
		btnIncrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count=Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toBinaryString(count+1));
			}
		});
	}*/
	//방법5(4번 방법의 해결책-onActivityCreated에서 구현 getview())
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Button btnIncrease=(Button) getView().findViewById(R.id.ch20_counterfragment_increase);
		final TextView textCounter=(TextView) getView().findViewById(R.id.ch20_counterfragment_counter);
		btnIncrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count=Integer.parseInt(textCounter.getText().toString());
				textCounter.setText(Integer.toString(count+1));
			}
		});
	}
}
